//
//  RadioModelListInfo.m
//  宝宝儿歌
//
//  Created by student on 16/4/23.
//  Copyright © 2016年 baobaoerge. All rights reserved.
//

#import "RadioModelListInfo.h"

#import <TFHpple.h>
#import <AFNetworking.h>

@implementation RadioModelListInfo

+ (void)radioModelListInfo:(NSString *)urlStr
               successBlok:(void (^)(NSArray *imgList))successBlock
                  failBlok:(void (^)(NSError *error))failBlock
{
    
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError)
     {
         if (!connectionError)
         {
             
         TFHpple *hpple = [TFHpple hppleWithHTMLData:data];
         NSString *xpath1 = @"//div/ul/li/div/div [@class='operate']/a [@class='forwardBtn']";//连接和名字
         NSString *xpath2 = @"//div/ul/li/div/span [@class='sound_playcount']";//次数
         NSString *xpath3 = @"//div/ul/li/div/div [@class='operate']/span";//date
         NSString *xpath4 = @"//div/div/div/div/div/div [@class='personal_body']";//soundId
         NSArray *arr1 = [hpple searchWithXPathQuery:xpath1];
         NSArray *arr2 = [hpple searchWithXPathQuery:xpath2];
         NSArray *arr3 = [hpple searchWithXPathQuery:xpath3];
         NSArray *arr4 = [hpple searchWithXPathQuery:xpath4];
         
         NSMutableArray *list1 = [NSMutableArray array];
         NSMutableArray *list2 = [NSMutableArray array];
         NSMutableArray *list3 = [NSMutableArray array];
         NSMutableArray *list4 = [NSMutableArray array];

             NSLog(@"%lu %lu",(unsigned long)arr1.count,(unsigned long)arr4.count);
         for(TFHppleElement *tmp in arr1)
         {
             NSMutableDictionary *tmpDict = [NSMutableDictionary dictionary];
             
             NSDictionary *dict = tmp.attributes;
             NSString *link = dict[@"track_id"];//连接
             NSString *title = dict[@"track_title"];//title
//             NSString *link2 = [NSString stringWithFormat:@"http://www.ximalaya.com/tracks/%@.json",link];//Json链接
             [tmpDict setObject:title forKey:@"radioTitle"];
             [tmpDict setObject:link forKey:@"radioSource"];
             [list1 addObject:tmpDict];
         }
         for (int i=0; i<list1.count; i++)
         {
             TFHppleElement *tmp = arr2[i];
             NSDictionary *tmpDict = list1[i];
             NSString *times = tmp.text;//次数
             [tmpDict setValue:times forKey:@"radioTimes"];
             [list2 addObject:tmpDict];
         }
         for (int i=0; i<list1.count; i++)
         {
             TFHppleElement *tmp = arr3[i];
             NSDictionary *tmpDict = list2[i];
             NSString *date = tmp.text;//date
             [tmpDict setValue:date forKey:@"radioDate"];
             [list3 addObject:tmpDict];
        }
         for (int i=0; i<list1.count; i++)
         {
             TFHppleElement *tmp = arr4[0];
             NSDictionary *dict = tmp.attributes;
             NSDictionary *tmpDict = list3[i];
             NSString *soundId = dict[@"sound_ids"];//soundId
             [tmpDict setValue:soundId forKey:@"radioSoundId"];
             [list4 addObject:tmpDict];
         }
//             NSLog(@"%@",list4);
         successBlock(list3);
    }
         else
         {
             NSLog(@"请求失败");
             failBlock(nil);
         }
         
     }];

}


@end
